#!/usr/bin/env python
# -*- coding:UTF-8 -*-


def singleton(cls):
    _instance = {}

    def inner(**argv):
        if cls not in _instance:
            _instance[cls] = cls(**argv)
        return _instance[cls]
    return inner


class SingleType(type):
    """
    普通类相当于是元类的实例，所以在类完成定义时其实是调用元类的__init__方法，而生成类的实例，如 Test('AIF')则是调用元类的__call__方法(debug可以跟踪到元类的__call__最后还是调用普通类的__init__方法)
    这个跟普通类和实例的关系其实是一样的。如 t = Test('AIF'), 生成t会调用Test的__init__方法，而t()则会调用Test的__call__方法
    """
    def __init__(self, *args, **kwargs):
        self._instance = {}
        super().__init__(*args, **kwargs)

    def __call__(self, *args, **kwargs):
        file_name = kwargs.get("name")
        if file_name is None:
            raise ValueError("name is can not empty!")
        if self._instance.get(file_name) is None:
            self._instance[file_name] = super().__call__(*args, **kwargs)
        return self._instance[file_name]

'''
'''
# @singleton
# class Cls(object):
#     def __init__(self):
#         pass
#
# cls1 = Cls()
# cls2 = Cls()
# print(id(cls1) == id(cls2))


